limsMaterialApply.auditSearchDetailPage = {

    getBtnToolbar: function (step) {
        var _this = this
        var buttons = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                workspace.window.goBack()
            }
        }]
        if (step) {
            if (step === 'audit') {
                buttons.push({
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.PASS',
                    icon: 'pass',
                    onClick: function () {
                        _this.passSingle()
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.WORKFLOW.BUTTON.REJECT',
                    icon: 'reject',
                    onClick: function () {
                        _this.rejectSingle()
                    }
                })
            }
        }
        return {
            type: 'btnToolbar',
            items: buttons
        }
    },

    getMaterialOutGrids: function (applyId) {
        let _this = this
        return {
            type: 'tab',
            panels: [{
                title: 'T_LIMS_MATERIAL_OUT.SERVICE_NAME',
                items: [{
                    type: 'grid',
                    id: _this.idManager.materialOutGrid,
                    service: 'limsMaterialOutServiceImpl',
                    dbTable: 'T_LIMS_MATERIAL_OUT',
                    columns: limsMaterialApply.getMaterialOutSearchListColumns(),
                    onLoadSuccess: function (data) {
                        if (!Gikam.isEmpty(data)) {
                            this.activeRowByIndex(0)
                        } else {
                            // _this.refreshInventoryTabs(null)
                        }
                    },
                    onRowActive: function (index, row) {
                        // _this.refreshInventoryTabs(row)
                    },
                    url: _this.urlManager.materialOutGrid + '/queries',
                    requestData: {
                        applyId_EQ: applyId
                    },
                    toolbar: []
                }]
            }, {
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: _this.idManager.materialOutGrid + '-uploader',
                    filter: false,
                    dbTable: 'T_LIMS_MATERIAL_APPLY',
                    bizId: applyId,
                    readonly: true
                }]
            },{ //审核记录
                title: 'T_CORE_FILE.OPERATE.OPERATION.RECORD',
                items: [{
                    type: 'grid',
                    filter: false,
                    page: false,
                    toolbarHidden: true,
                    url: IFM_CONTEXT + '/secure/core/module/sys/bpmn-comments/queries?table=T_LIMS_MATERIAL_APPLY&id=' + applyId,
                    columns: limsMaterialApply.getFlowListColumns()
                }]
            }]
        }
    },

    refreshInventoryTabs: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.inventoryGrid).done(grid => {
            if (row && row.id) {
                grid.refresh({
                    url: _this.urlManager.inventoryGrid + '/queries',
                    requestData: {
                        matCode_EQ: row.matCode
                    }
                })
            } else {
                grid.refresh({
                    url: _this.urlManager.inventoryGrid + '/queries',
                    requestData: {
                        matCode_EQ: -1
                    }
                })
            }
        })
    },

    getInventoryGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.inventoryGrid,
            service: 'limsStandardInventoryServiceImpl',
            dbTable: 'T_LIMS_STANDARD_INVENTORY',
            columns: limsMaterialApply.getStandardInventoryEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshHistory(null)
                }
            },
            deleteFormatter: function (row) {
                return row.storageNo || row.id
            },
            onRowActive: function (index, row) {
                _this.refreshHistory(row)
            },
            toolbar: [{
                type: 'button',
                text: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                icon: 'export',
                hidden: !_this.param.editAble,
                onClick: function () {
                    var grid = Gikam.getComp(_this.idManager.inventoryGrid)
                    var activedRow = grid.getActivedRow()
                    if (Gikam.isEmpty(activedRow)) return
                    if (!activedRow.ext$.matexpired || activedRow.ext$.matexpired === '1') {
                        Gikam.alert('BASEMODULE.RM.MATERIALS.TIP.USEFULTIME_PASSED')
                        return
                    }
                    var model = Gikam.create('modal', {
                        title: 'BASEMODULE.RM.MATERIALS.BUTTON_CHECKOUT',
                        height: '250',
                        width: '400',
                        onAfterClose: function (data) {
                            if (Gikam.isEmpty(data)) {
                                return
                            }
                        }
                    })
                    var applyGrid = Gikam.getComp(_this.idManager.materialApplyGrid)
                    Gikam.create('layout', {
                        renderTo: model.window.$dom,
                        center: {
                            items: [{
                                type: 'form',
                                id: 'insert-checkout-form',
                                titleWidth: 70,
                                fields: limsMaterialApply.getStandardInventoryOutFields(true),
                                columns: 1,
                                onLoadSuccess: function () {
                                }
                            }, {
                                type: 'btnToolbar',
                                items: [{
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CONFIRM',
                                    class: 'blue',
                                    icon: 'save',
                                    color: 'white',
                                    onClick: function () {
                                        var form = Gikam.getComp('insert-checkout-form')
                                        if (form.validate()) {
                                            var data = form.getData()
                                            var url = _this.urlManager.inventoryGrid + '/standards/checkout'
                                            Gikam.postText(url, Gikam.getJsonWrapper({
                                                applyId: applyGrid.getActivedRow().id,
                                                applyType: applyGrid.getActivedRow().applyType
                                            }, [null, [activedRow]], [null, [data]])).done(function (data) {
                                                grid.refreshRowById(activedRow.id)
                                                _this.refreshHistory(activedRow)
                                                Gikam.getLastModal().close(data)
                                            })
                                        }
                                    }
                                }, {
                                    type: 'button',
                                    text: 'GIKAM.BUTTON.CANCEL',
                                    icon: 'back',
                                    onClick: function () {
                                        Gikam.getLastModal().close()
                                    }
                                }]
                            }]
                        }
                    })
                }
            },]
        }
    },

    getHistoryGrid: function () {
        let _this = this
        return {
            type: 'grid',
            id: _this.idManager.historyGrid,
            service: 'limsStandardHistoryServiceImpl',
            dbTable: 'T_LIMS_STANDARD_HISTORY',
            columns: limsMaterialApply.getStandardHistoryEditListColumns(),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                }
            },
            deleteFormatter: function (row) {
                return row.index + 1
            },
            onRowActive: function (index, row) {
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove',
                hidden: !_this.param.editAble,
                onClick: function () {
                    let rows = Gikam.getComp(_this.idManager.historyGrid).getSelections()
                    if (Gikam.isEmpty(rows)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                        return
                    }
                    var url = _this.urlManager.inventoryGrid + '/history'
                    Gikam.getComp(_this.idManager.historyGrid).deleteRows(url).done(function () {
                        Gikam.getComp(_this.idManager.historyGrid).refresh()
                    })
                }
            }]
        }
    },

    refreshHistory: function (row) {
        let _this = this
        Gikam.getAsyncComp(_this.idManager.historyGrid).done(grid => {
            if (row && row.id) {
                grid.refresh({
                    url: _this.urlManager.historyGrid + '/queries',
                    requestData: {
                        standardInventoryId_EQ: row.id,
                        applyId_EQ: _this.param.id
                    }
                })
            } else {
                grid.refresh({
                    url: _this.urlManager.historyGrid + '/queries',
                    requestData: {
                        standardInventoryId_EQ: -1
                    }
                })
            }
        })
    },

    idManager: {
        materialApplyGrid: 'lims-material-apply-audit-list-grid',
        materialOutGrid: 'lims-material-out-audit-list-grid',
        auditRecordGrid: 'lims-material-apply-audit-list-grid' + '-record',
        inventoryGrid: 'lims-standard-inventory-audit-list-grid',
        historyGrid: 'lims-standard-history-audit-list-grid'
    },

    urlManager: {
        materialApplyGrid: limsMaterialApply.baseUrl,
        materialOutGrid: IFM_CONTEXT + '/secure/basemodule/rm/material-outs',
        inventoryGrid: IFM_CONTEXT + '/secure/basemodule/rm/standard-inventorys',
        historyGrid: IFM_CONTEXT + '/secure/basemodule/rm/standard-historys'
    },

    passSingle: function () {
        let _this = this
        Gikam.create('workflow').pass({
            data: [{id: _this.param.id}],
            pageObject: limsMaterialApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    rejectSingle: function () {
        let _this = this
        Gikam.create('workflow').reject({
            data: [{id: _this.param.id}],
            pageObject: limsMaterialApply,
            formatterToolbar: (toolbar, type, formId) => {
                toolbar.forEach(b => {
                    if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                        b.class = 'blue'
                        b.icon = 'save'
                        b.color = 'white'
                    } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                        b.icon = 'back'
                    }
                })
                return toolbar
            }
        }).done(function () {
            workspace.window.goBack()
        })
    },

    createPage: function () {
        let _this = this
        Gikam.create('layout', {
            center: {
                // height: '50%',
                items: [this.getBtnToolbar(_this.param.step), this.getMaterialOutGrids(_this.param.id)]
            },
            /*center: {
                items: [{
                    type: 'tab',
                    panels: [{
                        title: 'T_LIMS_MATERIAL_APPLY.OPERATION.CHECK_OUT_RECORD',
                        items: [{
                            type: 'layout',
                            west: {
                                width: '50%',
                                items: [_this.getInventoryGrid()]
                            },
                            center: {
                                items: [_this.getHistoryGrid()]
                            }
                        }]
                    },]
                }]
            },*/
            toolbarAlign: 'right',
            renderTo: workspace.window.$dom
        })
        return this
    },

    init: function (param) {
        this.param = param
        this.createPage()
    }
}
